package cn.itsource.system.controller;

import cn.itsource.blog.domain.ArticleType;
import cn.itsource.system.domain.Menu;
import cn.itsource.system.query.MenuQuery;
import cn.itsource.system.service.IMenuService;
import cn.itsource.util.JsonResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：菜单Menu
 */
@RestController
@RequestMapping(value = "/menu")
@Api(tags = "菜单接口类")
public class MenuController {
    @Autowired
    private IMenuService menuService;

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "查询所有")
    @GetMapping
    public List<Menu> findAll(){
        return menuService.findAll();
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Menu findOne(@PathVariable("id")Long id){
        return menuService.findOne(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id")Long id){
        try {
            menuService.delete(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 添加或修改
     * @param menu
     * @return
     */
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody Menu menu){
        try {
           if(menu.getId()==null){
               menuService.add(menu);
           }else{
               menuService.update(menu);
           }
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public JsonResult patchDel(@RequestBody Long[] ids){
        try {
            menuService.patchDel(ids);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 分页查询+高级查询
     * @param menuQuery
     * @return
     */
    @PostMapping
    public PageInfo<Menu> queryPage(@RequestBody MenuQuery menuQuery){
        return menuService.queryPage(menuQuery);
    }

    @ApiOperation(value = "查询菜单树")
    @GetMapping("/menuTree")
    public List<Menu> menuTree(){
        return menuService.menuTree();
    }
}
